unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdExplicitTLSClientServerBase, IdFTP, StdCtrls, AdvEdit, AdvEdBtn,
  AdvDirectoryEdit, FileCtrl, FlCtrlEx, ComCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    FTP: TIdFTP;
    Button2: TButton;
    AdvDirectoryEdit1: TAdvDirectoryEdit;
    FileListBoxEx1: TFileListBoxEx;
    Button3: TButton;
    StatusBar1: TStatusBar;
    Button4: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure AdvDirectoryEdit1Change(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FTPStatus(ASender: TObject; const AStatus: TIdStatus;
      const AStatusText: string);
    procedure Button4Click(Sender: TObject);
    procedure FTPWork(ASender: TObject; AWorkMode: TWorkMode;
      AWorkCount: Integer);
  private
    { Private declarations }
    IlePoszlo : longint;
    procedure SetMode(fn: string);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
uses IdFTPCommon;

procedure TForm1.AdvDirectoryEdit1Change(Sender: TObject);
begin
  FileListBoxEx1.Directory := AdvDirectoryEdit1.Text;
end;

procedure TForm1.Button1Click(Sender: TObject);
Var
  LS: TStringList;
  i : integer;
begin
  FTP.Connect;
   if FTP.Connected then
   begin
     ListBox1.Clear;
     try
       LS := TStringList.Create;

{       FTP.List(LS);
       LS.Sort;
       for i := 0 to LS.Count - 1 do
        LS[i] := copy(LS[i], 53, 200);


       ListBox1.Items.Assign(LS);}
       FTP.List(LS, '*.*', false);
       ListBox1.Items.Assign(LS);

     finally
       LS.Free;
     end;



   end;
  FTP.Disconnect;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  IlePoszlo := 0;
  SetMode(ListBox1.Items[ListBox1.ItemIndex]);
  FTP.Connect;
   if FTP.Connected then
   begin
     FTP.Get(ListBox1.Items[ListBox1.ItemIndex],
       AdvDirectoryEdit1.Text+'\'+ListBox1.Items[ListBox1.ItemIndex], true, false);
   end;
  FTP.Disconnect;
  FileListBoxEx1.Refresh;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
 name : string;
begin
  IlePoszlo := 0;
  name := ExtractFileName(FileListBoxEx1.FileName);
  SetMode(name);
  FTP.Connect;
   if FTP.Connected then
   begin
     FTP.Put(FileListBoxEx1.FileName, name, false);
   end;
  //StatusBar1.SimpleText := 'Koniec';
  FTP.Disconnect;
  Button1Click(Sender); 
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  Close;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  AdvDirectoryEdit1.Text := ExtractFilePath(Application.ExeName);
end;

procedure TForm1.FTPStatus(ASender: TObject; const AStatus: TIdStatus;
  const AStatusText: string);
begin
  StatusBar1.Panels[0].Text := AStatusText;
  StatusBar1.Refresh;
end;

procedure TForm1.FTPWork(ASender: TObject; AWorkMode: TWorkMode;
  AWorkCount: Integer);
begin
  if AWorkCount>IlePoszlo+1000 then
  begin
    StatusBar1.Panels[2].Text := IntToStr(AWorkCount);
    StatusBar1.Refresh;
    IlePoszlo := AWorkCount;
  end;
end;

procedure TForm1.SetMode(fn: string);
var
  ext : string;
begin
  ext := uppercase(ExtractFileExt(fn));
  if ext='.TXT' then
    FTP.TransferType := ftASCII
  else
  if (ext='.EXE') or
     (ext='.DLL') or
     (ext='.XLS') then
    FTP.TransferType := ftBinary;

  StatusBar1.Panels[1].Text := 'ASCI';
  if FTP.TransferType = ftBinary then
    StatusBar1.Panels[1].Text := 'Binary';
  StatusBar1.Refresh;
end;

end.
